package com.global.college.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.MainActivity;
import com.global.college.CollegeApplication;
import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseActivity;
import com.global.college.base.baselist.EventBusBean;
import com.global.college.constants.Constant;
import com.global.college.data.WeChatTokenBean;
import com.global.college.ui.bean.LoginModel;
import com.global.college.ui.bean.WechatLoginModel;
import com.global.college.ui.bean.WechatUserModel;
import com.global.college.ui.interfaces.ILoginView;
import com.global.college.ui.presenter.LoginPresenter;
import com.global.college.utils.LogUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.TitleBar;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 微信登录
 */
public class LoginByWechatActivity extends BaseActivity implements TitleBar.TitleBarCallback, ILoginView {


    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.tv_msg_1)
    TextView tvMsg1;
    @BindView(R.id.tv_msg_2)
    TextView tvMsg2;
    @BindView(R.id.tv_msg_3)
    TextView tvMsg3;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.cb_regist_lagel)
    CheckBox cbRegistLagel;
    @BindView(R.id.tv_regist_lagel)
    TextView tvRegistLagel;
    @BindView(R.id.tv_privacyPolicy)
    TextView tvPrivacyPolicy;
    @BindView(R.id.ll_login_by_sms)
    LinearLayout llLoginBySms;
    @BindView(R.id.ll_login_by_psw)
    LinearLayout llLoginByPsw;
    @BindView(R.id.ll_login)
    LinearLayout llLogin;
    private LoginPresenter loginPresenter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_login_by_wechat;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(false);
        EventBus.getDefault().register(this);
        titleBar.setTitleBarCallback(this);
    }

    @Override
    public void getInitData() {
        loginPresenter = new LoginPresenter(this);
        loginPresenter.setLoginView(this);
        initLiseners();
    }

    private void initLiseners() {
        cbRegistLagel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if (checked) {
                    tvLogin.setEnabled(true);
                    llLogin.setBackgroundResource(R.drawable.selector_button_red);
                    tvLogin.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvLogin.setEnabled(false);
                    llLogin.setBackgroundResource(R.drawable.selector_button_grey);
                    tvLogin.setTextColor(getResources().getColor(R.color.white));
                    ToastUtils.showCenterToast(R.string.success_this_terms_to_use_soft);
                }
            }
        });
    }

    @Override
    public void onBackPress() {
        finish();
    }

    @Override
    public void onRightPress() {

    }

    @Override
    public void onRightImageClick() {

    }

    @Override
    public void onSearchImageClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_login, R.id.tv_regist_lagel, R.id.tv_privacyPolicy, R.id.ll_login_by_sms, R.id.ll_login_by_psw, R.id.ll_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
            case R.id.ll_login:
                if (!cbRegistLagel.isChecked()) {
                    ToastUtils.showCenterToast(R.string.success_this_terms_to_use_soft);
                    return;
                }
                // 微信登陆
                break;
            case R.id.tv_regist_lagel:
                startActivity(new Intent(this, HsLagelDetailActivity.class).putExtra("type", "0"));
                break;
            case R.id.tv_privacyPolicy:
                startActivity(new Intent(this, HsLagelDetailActivity.class).putExtra("type", "1"));
                break;
            case R.id.ll_login_by_sms:
                openActivity(LoginByPswActivity.class);
                break;
            case R.id.ll_login_by_psw:
                openActivity(LoginByPswActivity.class);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCallBack(EventBusBean event) {
        LogUtils.w("event:" + event.getType());
        if (event != null) {
            switch (event.getType()) {
                case Constant.EventBus.WECHAT_LOGIN_SUCCESS:
                    finish();
                    break;
                case Constant.EventBus.BIND_SUCCESS:
                    finish();
                    break;


            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onLoginSuccess(LoginModel loginModel) {
        //请求成功，将返回值存入到本地缓存
        UserData.getInstance().setToken(loginModel.getToken());
        openActivity(MainActivity.class);

    }

    @Override
    public void onLoginFail(String msg) {

    }

    @Override
    public void onGetSmsCodeSuccess(Object smsModel) {

    }

    @Override
    public void onGetSmsCodeFail(String msg) {

    }

}